package org.apache.linkis.engineplugin.spark.parse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Calendar;
import java.util.Date;

public class VariableParser {


    public static void main(String[] args) {
        String sql1 = "select 1 ,${a},${b},$[yyyyMMdd(dd-1)],${bdc.system.bizdate} from tmp$[yyyyMMdd];";
        String res1 = parse(sql1);
        System.out.println(res1);

        String sql2 = "select 1 ,'${a}','${b}','$[yyyyMMdd(dd-1)]','${bdc.system.bizdate}' from tmp$[yyyyMMdd];";
        String res2 = parse(sql2);
        System.out.println(res2);
    }


    public static String parse(String string) {
        TokenHandler1 handler1 = new TokenHandler1();
        TokenHandler2 handler2 = new TokenHandler2();

        GenericTokenParser parser1 = new GenericTokenParser("${", "}", handler1);
        String res1 = parser1.parse(string);
        GenericTokenParser parser2 = new GenericTokenParser("$[", "]", handler2);
        String res2 = parser2.parse(res1);

        return res2;
    }


    private static class TokenHandler1 implements TokenHandler {
        @Override
        public String handleToken(String content) {
            return content;
        }
    }

    private static class TokenHandler2 implements TokenHandler {
        @Override
        public String handleToken(String content) {
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            return DateFormatUtils.format(DateUtils.addDays(now, -1), "yyyyMMdd");
        }
    }

    public static class GenericTokenParser {

        private final String openToken;
        private final String closeToken;
        private final TokenHandler handler;

        public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
            this.openToken = openToken;
            this.closeToken = closeToken;
            this.handler = handler;
        }

        public String parse(String text) {
            final StringBuilder builder = new StringBuilder();
            final StringBuilder expression = new StringBuilder();
            if (text != null && text.length() > 0) {
                char[] src = text.toCharArray();
                int offset = 0;
                // search open token
                int start = text.indexOf(openToken, offset);
                while (start > -1) {
                    if (start > 0 && src[start - 1] == '\\') {
                        // this open token is escaped. remove the backslash and continue.
                        builder.append(src, offset, start - offset - 1).append(openToken);
                        offset = start + openToken.length();
                    } else {
                        // found open token. let's search close token.
                        expression.setLength(0);
                        builder.append(src, offset, start - offset);
                        offset = start + openToken.length();
                        int end = text.indexOf(closeToken, offset);
                        while (end > -1) {
                            if (end > offset && src[end - 1] == '\\') {
                                // this close token is escaped. remove the backslash and continue.
                                expression.append(src, offset, end - offset - 1).append(closeToken);
                                offset = end + closeToken.length();
                                end = text.indexOf(closeToken, offset);
                            } else {
                                expression.append(src, offset, end - offset);
                                offset = end + closeToken.length();
                                break;
                            }
                        }
                        if (end == -1) {
                            // close token was not found.
                            builder.append(src, start, src.length - start);
                            offset = src.length;
                        } else {
                            builder.append(handler.handleToken(expression.toString()));
                            offset = end + closeToken.length();
                        }
                    }
                    start = text.indexOf(openToken, offset);
                }
                if (offset < src.length) {
                    builder.append(src, offset, src.length - offset);
                }
            }
            return builder.toString();
        }
    }
}
